<?php

define('APC_TIMEOUT', 1);
require_once(dirname(__FILE__) . '/../lib/core/dao/daostatement.class.php');
require_once(dirname(__FILE__) . '/../lib/core/dao/dao.class.php');
require_once(dirname(__FILE__) . '/../lib/core/dao/mysql.class.php');
require_once(dirname(__FILE__) . '/../lib/core/dao/db.class.php');
require_once(dirname(__FILE__) . '/../lib/core/yaml/sfYamlParser.php');
require_once(dirname(__FILE__) . '/../lib/core/ecEnvironment.class.php');
require_once(dirname(__FILE__) . '/../lib/core/ecConnectionDatabase.class.php');
require_once(dirname(__FILE__) . '/../lib/core/util/sfInflector.class.php');
require_once(dirname(__FILE__) . '/../lib/core/util/sfToolkit.class.php');

ecEnvironment::init('dev', false);
ecConnectionDatabase::init();

$master = ecConnectionDatabase::getDbMaster();

$req = "show tables";
$result = $master->query($req);

$tabTables = $result->fetchAllArray();

$tailleTable = sizeof($tabTables);

foreach ($tabTables as $keys => $tabvalue)
{
  foreach ($tabvalue as $key => $value)
  {
    echo "<b>" . $value . "</b><br /> \n";
    $sql = "SHOW COLUMNS FROM " . $value;

    $res = $master->query($sql);

    $tabColumns = $res->fetchAllAssoc();

    $handle = "<?php
";

    $handle .= "class #table# extends ec#extends#{
    public \$tableName = '#table_normalise#';

		#fieldsVars#

    public function  __construct(\$array = array(), \$init = array(#fields#))
    {
      \$this->init(\$array, \$init);
      parent::__construct(\$array);
    }

		#fieldsSets#

		#fieldsGets#

    public function add#tableUc#()
    {
      return parent::add(\$this->tableName, \$this->getValuesArray());
    }

    public function update#tableUc#(\$conditions = array())
    {
      return parent::update(\$this->tableName, \$this->getValuesArray(), \$conditions);
    }

    public function delete#tableUc#(\$conditions = array())
    {
      return parent::delete(\$this->tableName, \$conditions);
    }

    public function load#tableUc#(\$key=array('*'), \$conditions= array(), \$object='slave')
    {
      \$tri = '';
      \$request = parent::select(\$this->tableName,\$key,\$conditions, \$object ,\$tri)->fetchAssoc();

      if(is_object(\$request))
        return \$request->fetchAllAssoc();
      else
        return false;
    }

    public function loadAll#tableUc#(\$key = array('*'), \$conditions= array(), \$tri='', \$object='slave')
    {
      \$request = parent::select(\$this->tableName,\$key,\$conditions, \$object ,\$tri);

      if(is_object(\$request))
        return \$request->fetchAllAssoc();
      else
        return false;
    }

}";

    $handle = str_replace("#table#", lcfirst(sfInflector::camelize($value)), $handle);
    $handle = str_replace("#table_normalise#", $value, $handle);
    $handle = str_replace("#tableUc#", sfInflector::camelize($value), $handle);

    $field = "";
    $fieldsSets = "";
    $fieldsGets = "";
    $fieldsVars = "";
    $state = 0;
    foreach ($tabColumns as $keysColumns => $tabValues)
    {
      if ($tabValues['Field'] == 'created_at' OR
              $tabValues['Field'] == 'updated_at' OR
              $tabValues['Field'] == 'deleted_at' OR
              $tabValues['Field'] == 'state')
      {
        $state = 1;
      }
      else
      {
        $fieldSet = "public function set#fieldUc#(\$value)
    {
      if(Validator::getValidator('#type#', \$value))
        \$this->#field# = \$value;
      else
      {
        array_push(\$this->arrayErrors, 'keys #field# is not valid');
        return false;
      }
    }

    ";
        $fieldGet = "public function get#fieldUc#()
    {
      return \$this->#field#;
    }

    ";
        $fieldsVars .='protected $' . lcfirst(sfInflector::camelize($tabValues['Field'])) . ';
    ';

        $fieldSet = str_replace("#fieldUc#", sfInflector::camelize($tabValues['Field']), $fieldSet);
        $fieldSet = str_replace("#field#", lcfirst(sfInflector::camelize($tabValues['Field'])), $fieldSet);

        $pos = strpos($tabValues['Type'], "(");
        if ($pos > 0)
          $fieldSet = str_replace("#type#", substr($tabValues['Type'], 0, strpos($tabValues['Type'], "(")), $fieldSet);
        else
          $fieldSet = str_replace("#type#", $tabValues['Type'], $fieldSet);
        $fieldsSets .= $fieldSet;

        $fieldGet = str_replace("#fieldUc#", sfInflector::camelize($tabValues['Field']), $fieldGet);
        $fieldGet = str_replace("#field#", lcfirst(sfInflector::camelize($tabValues['Field'])), $fieldGet);
        $fieldsGets .= $fieldGet;

        $field .= "'" . $tabValues['Field'] . "' => ";

        if ($tabValues['Null'] == 'YES')
        {
          $field .= "0,";
        }
        else
        {
          $field .= "1,";
        }

        echo $tabValues['Field'] . " \n";
        echo $tabValues['Type'] . " \n";
        echo $tabValues['Null'] . " \n";
        echo $tabValues['Key'] . " \n";
        echo $tabValues['Extra'] . "<br />\n";
      }
    }
    $handle = str_replace("#fieldsVars#", $fieldsVars, $handle);
    $handle = str_replace("#fieldsSets#", $fieldsSets, $handle);
    $handle = str_replace("#fieldsGets#", $fieldsGets, $handle);
    $handle = str_replace("#fields#", $field, $handle);
    if ($state > 0)
      $handle = str_replace("#extends#", 'TraceTemp', $handle);
    else
      $handle = str_replace("#extends#", 'Base', $handle);

    file_put_contents(dirname(__FILE__) . '/../lib/class/base/' . lcfirst(sfInflector::camelize($value)) . '.class.php', $handle);
    chmod(dirname(__FILE__) . '/../lib/class/base/' . lcfirst(sfInflector::camelize($value)) . '.class.php', 0775);
    echo "<br/>\n";
  }
}
?>
